package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vehicle.bean.Menu;
import com.vehicle.mapper.MenuMapper;
import com.vehicle.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.vo.MenuListVO;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-18
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<MenuListVO> getAllListMenu() {

        List<MenuListVO> menuListVOS = new ArrayList<>();
        //首先获取一级菜单
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_id");
        queryWrapper.eq("pid",0);
        List<Menu> menuList = menuMapper.selectList(queryWrapper);
        for (Menu m:menuList) {
            MenuListVO menuListVO = new MenuListVO();
            BeanUtils.copyProperties(m,menuListVO);
            QueryWrapper<Menu> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.orderByAsc("order_id");
            queryWrapper2.eq("pid",m.getMid());
            List<Menu> menus = menuMapper.selectList(queryWrapper2);
            menuListVO.setChildren(menus);
            menuListVOS.add(menuListVO);
        }
        return menuListVOS;
    }
}
